import React, { useEffect, useState } from "react";
import { Table, Button } from "antd";
import { calculateTableWidth, downloadFile } from "@/utils/util";
import { exportInWhData, exportInWhDataPreview, confirmOrderInWhData } from "../services";
import { inWarehouseType } from "@/constants/types";
import { useRequest } from "ahooks";
import WarehouseEntryExport from "../modals/WarehouseEntryConfirm";

const WarehouseEntryVerify = () => {
  const searchParams = new URLSearchParams(location.search);
  const ids = searchParams.get('ids') || "";
  const [dataSource, setDataSource] = useState<any []>([]);
  
  //
  const {loading, runAsync} = useRequest(confirmOrderInWhData, {manual: true}) 
  const [currentRow, setCurrentRow] = useState<inWarehouseType | null>(null);
  const [visible, setVisible] = useState<boolean>(false);

  const search = () => {
    exportInWhDataPreview({ids: ids.split(',')}).then(
      res => {
        setDataSource(res || []);
      }
    );
  }

  const editProps = {
    title: "导出入仓核实单规格确认",
    visible,
    currentRow,
    loading,
    onCancel: () => {
      setVisible(false);
      setCurrentRow(null);
    },
    onSubmit: async (params:any) => {
      const res = await runAsync(params);
      if (res?.code === '0') {
        search();
        editProps.onCancel();
      }
    }
  }

  const handleaBatchExport = () => {
    exportInWhData({ids: ids?.split(',')}).then(
      res => {
        if (res) {
          downloadFile(res);
        }
      }
    )
  }
  const handleEdit = (record: inWarehouseType) => {
    setCurrentRow(record);
    setVisible(true);
  }

  const columns = [
    {
      dataIndex: "inWhDate",
      title: "入仓时间",
      width: 180
    },
    {
      dataIndex: "productName",
      title: "渠道",
      width: 120,
    },
    {
      dataIndex: "orderCode",
      title: "运单号",
      width: 150
    },
    {
      dataIndex: "destCode",
      title: "仓库代码",
      width: 120
    },
    {
      dataIndex: "shipmentId",
      title: "Ship ID",
      ellispis: true,
      width: 150
    },
    {
      dataIndex: "inWhPieces",
      title: "件数",
      width: 120
    },
    {
      dataIndex: "balanceVol",
      title: "实际体积",
      width: 120,
      render: (_:any, record: inWarehouseType) => <Button type="link" onClick={() => handleEdit(record)}>{record?.balanceVol}</Button>
    },
    {
      dataIndex: "balanceWeight",
      title: "实际重量",
      width: 120,
      render: (_:any, record: inWarehouseType) => <Button type="link" onClick={() => handleEdit(record)}>{record?.balanceWeight}</Button>
    },
    {
      dataIndex: "feeVol",
      title: "计费体积",
      width: 120
    },
    {
      dataIndex: "feeWeight",
      title: "计费重量",
      width: 120
    },
    {
      dataIndex: "price",
      title: "单价/元",
      width: 120
    },
    {
      dataIndex: "bgFee",
      title: "报关费",
      width: 120
    },
    {
      dataIndex: "pickupFee",
      title: "提货费",
      width: 120
    },
    {
      dataIndex: "taxFee",
      title: "税金",
      width: 120
    },
    {
      dataIndex: "",
      title: "商检费",
      width: 120
    },
    {
      dataIndex: "cargoCheckFee",
      title: "保险费",
      width: 120
    },
    {
      dataIndex: "additionalFee",
      title: "附加费",
      width: 120
    },
    {
      dataIndex: "discountAmount",
      title: "优惠券",
      width: 120
    },
    {
      dataIndex: "totalAmount",
      title: "预估费用",
      width: 120
    },
  ];

  useEffect(() => {
    if (ids && location.pathname === '/order-manage/warehouse-entry-verify') {
      search();
    } else {
      setDataSource([]);
    }
  }, [ids])

  return (
    <>
      <div style={{display: 'flex', justifyContent:'flex-start', marginBottom: 10}}>
        <Button type="primary" onClick={handleaBatchExport}>确定导出</Button>
      </div>
      <Table
        bordered
        rowKey="id" 
        columns={columns}
        dataSource={dataSource}
        scroll={{x: calculateTableWidth(columns), y: 500}}
      />
      {visible && <WarehouseEntryExport {...editProps} />}
    </>
  );
}
export default WarehouseEntryVerify;